package com.example.travelhelper.controller;

import com.example.travelhelper.mapper.AdminMapper;
import com.example.travelhelper.pojo.Admin;
import com.example.travelhelper.service.serviceImpl.AdminServiceImpl;
import com.example.travelhelper.utils.JwtUtil;
import com.example.travelhelper.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private AdminMapper adminMapper;
    /**
     * 登录数据验证
     */
    @PostMapping("/login")
    @ResponseBody
    public ResponseData login(@RequestParam("aName") String aName, @RequestParam("aPassword") String aPassword) {
        System.out.println(aName);
        System.out.println(aPassword);
        Admin admin = adminService.login(aName, aPassword);
        if (admin != null) {
            Map<String,String> map = new HashMap<>();
            map.put("aName", admin.getAUsername());
            map.put("aId", String.valueOf(admin.getAId()));
            //登录成功生成Jwttoken
            String token = JwtUtil.getToken(map);
            map.put("token", token);
            return ResponseData.success("登录成功", map);
        } else {
            return ResponseData.fail("登录失败，密码或账号错误");
        }
    }
}
